package org.jinyan.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import org.apache.log4j.Logger;

/**
 * 客户端工具类 主要功能 1.用来测试规则应用表达式和文档类型表达式 2.读取或写入配置文件夹的配置信息的工具方法
 * 目录必须是/configure/目录下的Properties文件或html文件
 * 
 * @version 1.1
 * @author JinYan
 * 
 */

public class CommonUtils {

	private static Logger logger = Logger.getLogger(CommonUtils.class);

	/**
	 * 读取配置文件根据key读取value
	 * 
	 * @param filePath
	 *            文件路径
	 * @param key
	 *            指属性文件 key值
	 * @return
	 * @throws Exception
	 */

	public static String readFromProperties(String filePath, String key)
			throws Exception {
		Properties props = new Properties();
		InputStream in = new BufferedInputStream(new FileInputStream(filePath));
		props.loadFromXML(in);
		String value = props.getProperty(key);
		in.close();
		return value;
	}

	/**
	 * 读取properties的全部信息存放在map中 key-value
	 * 
	 * @param filePath
	 * @return
	 * @throws IOException
	 */

	public static Map readAllPropertiesToMap(String filePath)
			throws IOException {
		Map resultMap = new HashMap<String, String>();
		Properties props = new Properties();
		InputStream in = new BufferedInputStream(new FileInputStream(filePath));
		props.loadFromXML(in);
		Enumeration en = props.propertyNames();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			String propertyValue = props.getProperty(key);
			resultMap.put(key, propertyValue);
		}
		in.close();
		return resultMap;
	}

	/**
	 * 写入properties信息
	 * 
	 * @param filePath
	 * @param parameterName
	 * @param parameterValue
	 * @throws IOException
	 */

	public static void writeProperties(String filePath, String parameterName,
			String parameterValue) throws IOException {
		Properties prop = new Properties();
		String pathTemp = System.getProperty("user.dir") + "/configure/"
				+ filePath;
		File file = new File(pathTemp);
		if (file.exists() && file.isFile()) {
			InputStream fis = new FileInputStream(pathTemp);
			// 从输入流中读取属性列表（键和元素对）
			if (fis != null)
				prop.loadFromXML(fis);
			fis.close();
		}

		// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
		OutputStream fos = new FileOutputStream(pathTemp);
		prop.setProperty(parameterName, parameterValue);
		// 以适合使用 load 方法加载到 Properties 表中的格式，
		// 将此 Properties 表中的属性列表（键和元素对）写入输出流
		prop.storeToXML(fos, "系统设置", "utf-8");
		fos.close();
	}

	public static String getPackagePath(String filename) {
		String result = null;
		URL urlpath = CommonUtils.class.getResource("");
		String path = urlpath.toString();
		if (path.startsWith("file")) {
			path = path.substring(6);
		}

		if (path.indexOf("classes") > 0) {
			path = path.substring(0, path.indexOf("classes") - 1);
		}
		result = path + filename;
		return result;
	}
	
	public static String getUUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}
	

	public static String convertDtToChar14(Date date){
		if(date == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}
	public static String convertDtToChar8(Date date){
		if(date == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
}
